using System;
using System.Collections.Generic;
using Sustainalytics.ESG.Entities;
using Sustainalytics.Utils;

namespace Sustainalytics.ESGRatings.BusinessLogic
{
    public class IndicatorTemplateMapper
    {
        public bool Convert(IndicatorTemplate indicatorTemplate, out Computing.Entities.IndicatorTemplate indicator)
        {
            var indicatorTheme = this.GetThemeSymbol(indicatorTemplate.ThemeName, indicatorTemplate.Number);

            if (indicatorTheme.IsNullOrEmpty())
            {
                indicator = null;
                return false;
            }

            indicator = new Computing.Entities.IndicatorTemplate(indicatorTemplate.Number, indicatorTheme, this.GetClusterSymbol(indicatorTemplate.IndicatorCategory));

            return true;
        }

        private string GetClusterSymbol(string indicatorCluster)
        {
            var mappingDictionary = new Dictionary<string, string>
                                        {
                                            {"Disclosure","D"},
                                            {"Qualitative Performance - Controversies", "QLP"},
                                            {"Quantitative Performance", "QP"},
                                            {"Preparedness","P"}
                                        };

            return mappingDictionary.ContainsKey(indicatorCluster) ? mappingDictionary[indicatorCluster] : string.Empty;
        }

        private string GetThemeSymbol(string indicatorTheme, string indicatorNumber)
        {
            var theme = String.Empty;
            var acceptedThemeCodesSet = new HashSet<string>()
                                            {
                                                {"E"},
                                                {"S"},
                                                {"G"},
                                            };
            var themeNameThemeCodeMappingDictionary = new Dictionary<string, string>
                                                          {
                                                              {"Environment", "E"},
                                                              {"Social", "S"},
                                                              {"Governance", "G"},
                                                          };

            if (indicatorNumber.IsNullOrEmpty() && indicatorTheme.IsNullOrEmpty())
                throw new ArgumentOutOfRangeException(indicatorNumber, "Invalid theme code or code name exception");

            if (!indicatorNumber.IsNullOrEmpty())
            {
                if (acceptedThemeCodesSet.Contains(indicatorNumber.Substring(0, 1).ToUpperInvariant()))
                {
                    theme = indicatorNumber.Substring(0, 1).ToUpperInvariant();
                }
            }

            if (indicatorTheme.IsNullOrEmpty() && theme.IsNullOrEmpty())
            {
                themeNameThemeCodeMappingDictionary.TryGetValue(indicatorTheme, out theme);
            }
            return theme;
        }
    }
}